package com.oozol.lieluyun.config;

import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {
   @ExceptionHandler({Exception.class})
   @ResponseBody
   public Object defultExcepitonHandler(HttpServletRequest request, Exception e) {
      if (e instanceof BusinessException) {
         System.out.println("业务异常：" + e.getMessage() + " " + request.getMethod() + " " + request.getRequestURI());
         BusinessException be = (BusinessException)e;
         return new ErrCode(be.getErrCode().getCode(), be.getMessage());
      } else {
         e.printStackTrace();
         System.out.println("我报错了：" + e.getLocalizedMessage());
         System.out.println("我报错了：" + e.getCause());
         System.out.println("我报错了：" + e.getSuppressed());
         System.out.println("我报错了：" + e.toString());
         System.out.println("我报错了：" + e.getStackTrace()[0]);
         String err = "";
         if (e.getCause() == null) {
            err = e.toString();
         } else {
            err = e.getCause() + "";
         }

         return new ErrCode(500, err);
      }
   }
}
